package com.ljs.vuetest.controller;

import com.ljs.vuetest.bean.Student;
import com.ljs.vuetest.service.StudentService;
import com.ljs.vuetest.util.JsonData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class NoteController {
    @Resource
    StudentService studentService;
    @RequestMapping("/note")
    public  String note(){
        return  "note";
    }
    @RequestMapping("/vue")
    public  String vue(){
        return  "vue";
    }
    @RequestMapping("/axios")//访问axios页面
    public  String axios(){
        return  "axios";
    }
    @RequestMapping("/music")//访问axios页面
    public  String music(){
        return  "yuemusic";
    }
    @RequestMapping("/get")//测试get请求
    @ResponseBody
    public  String get(){
        return  "get成功了";
    }
    @RequestMapping("/post")//测试get请求
    @ResponseBody
    public  String post(){
        return  "post成功了";
    }
    @RequestMapping("/selall")
    @CrossOrigin //允许跨域的，可以用vue
    @ResponseBody
    public JsonData selall(@RequestParam("index") int index,@RequestParam("limit") int limit){
        JsonData jsonData=new JsonData();
      List<Student> studentList= studentService.selall(index,limit);
        jsonData.setData(studentList);
        jsonData.setMsg("传过去了");
        return  jsonData;
    }

    @RequestMapping("/selall/{name}")
    @ResponseBody
    public JsonData selbyname(@PathVariable("name") String name){
        JsonData jsonData=new JsonData();
        List<Student> studentList=null;
        if(!name.equals("")){
           studentList= studentService.selbyname(name);
        }

        jsonData.setData(studentList);
        jsonData.setMsg("按名字查找传过去了");
        return  jsonData;
    }
    //添加学生的方法
    @RequestMapping("/add")
    @ResponseBody
    public  String add(Student student){
        boolean flag=false;
        flag=   studentService.add(student);
        if(flag){
            return "添加成功";
        }else {
            return "添加失败";
        }

    }
}
